'''

Case Type： 数据库系统
Case Name： stat文件无权限
Create At:  2020/06/30
Owner    :  @li-xin12345
Descption:  1.集群正常运行临时表/sql间并行/作业重跑业务2.修改gaussdb.stat文件属主为root

history：
created： 2020/06/30  created by @li-xin12345
'''

import os
import sys
import unittest
from yat.test import Node
from yat.test import macro

sys.path.append(sys.path[0] + "/../")
from testcase.utils.Logger import Logger
from testcase.utils.Constant import Constant
from testcase.utils.CommonSH import *
from testcase.utils.Common import Common


class PortOccupied(unittest.TestCase):
    sh = CommonSH()
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info('------------Opengauss_Reliability_Dbsys_Case094.py start------------')
        self.SOURCE_PATH = macro.DB_ENV_PATH
        self.DN1_PATH = macro.DB_INSTANCE_PATH
        self.SCRIPTS_PATH = macro.SCRIPTS_PATH
        self.state_file_path = os.path.join(macro.DB_INSTANCE_PATH, macro.STATE_FILE_NAME)
        self.sqlfile1 = 'bx1.sql'
        self.sqlfile2 = 'bx2.sql'

        self.rootNode = Node()
        self.dbUserNode = Node(node='dbuser')

        self.com = Common()
        self.Constant = Constant()

    def test_set_source_start(self):
        # 目标主机新建路径
        self.to_path = os.path.dirname(self.DN1_PATH) + '/test_script/'
        # 目标主机文件路径
        self.newsql_path1 = os.path.join(self.to_path, self.sqlfile1)
        self.newsql_path2 = os.path.join(self.to_path, self.sqlfile2)
        # 拷贝文件
        self.com.scp_file(self.rootNode, self.sqlfile1, self.to_path)
        self.com.scp_file(self.rootNode, self.sqlfile2, self.to_path)

        # 执行sql并行脚本
        self.log.info('----------------------------执行sql并行脚本-----------------------------')
        sql_cmd = f'''
                   source {self.SOURCE_PATH};
                   gsql -d {self.dbUserNode.db_name}  -p {self.dbUserNode.db_port} -r -f {self.newsql_path1} &
                   gsql -d {self.dbUserNode.db_name}  -p {self.dbUserNode.db_port} -r -f {self.newsql_path2} &
                   wait
                   '''
        self.log.info(sql_cmd)
        sql_msg = self.dbUserNode.sh(sql_cmd).result()
        self.log.info(sql_msg)
        for error_msg in self.Constant.SQL_WRONG_MSG:
            self.assertNotIn(error_msg, sql_msg)

        # 获取文件属主属组
        self.log.info('----------------------------获取文件属主属组-----------------------------')
        get_owner_cmd = f"ls -l {self.state_file_path}"
        owner = self.rootNode.sh(get_owner_cmd).result()
        self.owner = owner.split()[2]
        self.log.info(f'owner: {owner}')
        get_grp_cmd = f"ls -l {self.state_file_path}"
        grp = self.rootNode.sh(get_grp_cmd).result()
        self.grp = grp.split()[3]
        self.log.info(f'grp: {grp}')

        # 更改文件属组
        self.log.info('----------------------------更改文件属组-----------------------------')
        ch_cmd = f'chown root:root {self.state_file_path}'
        self.log.info(f'ch_cmd: {ch_cmd}')
        ch_msg = self.rootNode.sh(ch_cmd).result()
        self.log.info(ch_msg)
        self.assertEqual(ch_msg, '')

        # 再次执行sql并行脚本
        self.log.info('----------------------------执行sql并行脚本-----------------------------')
        sql_cmd = f'''
                   source {self.SOURCE_PATH};
                   gsql -d {self.dbUserNode.db_name} -p {self.dbUserNode.db_port} -r -f {self.newsql_path1} &
                   gsql -d {self.dbUserNode.db_name} -p {self.dbUserNode.db_port} -r -f {self.newsql_path2} &
                   wait
                   '''
        self.log.info(sql_cmd)
        sql_msg = self.dbUserNode.sh(sql_cmd).result()
        self.log.info(sql_msg)
        for error_msg in self.Constant.SQL_WRONG_MSG:
            self.assertNotIn(error_msg, sql_msg)

    def tearDown(self):
        self.log.info('----------------------------后置处理-----------------------------')
        # 恢复文件属组
        self.log.info('----------------------------恢复文件属组-----------------------------')
        rech_cmd = f'chown {self.owner}:{self.grp} {self.state_file_path}'
        self.log.info(f'rech_cmd: {rech_cmd}')
        recover_msg = self.rootNode.sh(rech_cmd).result()
        self.log.info(recover_msg)

        # 删除scp的脚本
        self.com.scp_file(self.rootNode, self.sqlfile1, self.to_path, True)
        self.com.scp_file(self.rootNode, self.sqlfile2, self.to_path, True)

        db_status = self.sh.getDbClusterStatus('status')
        if db_status:
            pass
        else:
            raise Exception("db status is not true, please check!")
        self.log.info(f'db_status: {db_status}')
        self.log.info('----------------------Opengauss_Reliability_Dbsys_Case094 执行完成---------------------')
